<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addcate">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格区 -->
      <tree-table
        show-index
        index-text="#"
        :data="catelist"
        :columns="columns"
        border
        :show-row-hover="false"
        :selection-type="false"
        :expand-type="false"
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-check" v-if="scope.row.cat_del === true"></i>
          <i class="el-icon-close" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-if="scope.row.cat_level === 2" type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit"></el-button>
          <el-button type="danger" icon="el-icon-delete"></el-button>
        </template>
      </tree-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="dialogVisible"
      width="30%"
      @close="addClose"
    >
      <el-form
        :model="addcateList"
        :rules="rules"
        ref="ruleFormRef"
        label-width="100px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="addcateList.name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" prop="name">
          <!-- option数据源 ，props用来指定配置项 -->
          <el-cascader
            :options="parenLists"
            :props="parentProps"
            @change="parentChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="SureAddCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      catelist: [
        {
          cat_id: 1,
          name: '大家电',
          cat_pid: 0,
          cat_level: 0,
          cat_del: false,
          children: [
            {
              cat_id: 2,
              name: '电视',
              cat_pid: 1,
              cat_level: 1,
              cat_del: false,
              children: [
                {
                  cat_id: 2,
                  name: '曲面电视',
                  cat_pid: 3,
                  cat_level: 2,
                  cat_del: false,
                },
              ],
            },
          ],
        },
      ],
      total: 0,
      // 查询参数
      querInfo: {
        tynum: 1,
        pagenum: 3,
        pagesize: 5,
      },
      // 列
      columns: [
        {
          label: '分类名称',
          prop: 'name',
        },
        // 自定义列
        {
          label: '是否有效',
          //  定义当前这一列使用模板名称
          type: 'template',
          template: 'isok',
        },
        {
          label: '排序',
          type: 'template',
          template: 'order',
        },
        // 操作
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
      // 对话跨显示与否
      dialogVisible: false,
      // 添加分类
      addcateList: {
        name: '',
        cat_pid: 0,
        cat_level: 0,
      },
      // 验证规则
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
      },
      // 父级数据
      parenLists: [],
      // 级联选择器的配置对象
      parentProps: {
        value: 'cat-id',
        label: 'name',
        children: 'children',
      },
    }
  },
  created() {
    // this.getGoodsList()
  },
  methods: {
    // 获取商品数据
    async getGoodsList() {
      const { data: res } = await this.$http.get('catagories', {
        params: this.querInfo,
      })
      if (res.meta.state !== 200) {
        return this.message.console.error('获取数据失败')
      }
      this.catelist = res.data.result
      this.total = res.data.total
    },
    // 监听size
    handleSizeChange(val) {
      this.querInfo.pagesize = val
      this.getGoodsList()
    },
    // 监听pagenum
    handleCurrentChange(val) {
      this.querInfo.pagenum = val
      this.getGoodsList()
    },
    // 添加分类 按钮
    addcate() {
      // 先获取父级分类数据
      this.getParentCate()
      this.dialogVisible = true
    },
    // 获取父级分类
    async getParentCate() {
      const { data: res } = await this.$http.post('categories', {
        params: { type: 2 },
      })
      if (res.meta.state !== 200) {
        return this.$message.error('获取父级数据失败')
      }
      this.parenLists = res.data
    },
    // 选中的父级分类
    parentChange() {
      if (this.selKeys.length > 0) {
        this.addcateList.cat_id = this.selKeys[this.selKeys.length - 1]
        this.addcateList.cat_level = this.selKeys.length
      } else {
        this.addcateList.cat_pid = 0
        this.addcateList.cat_level = 0
      }
    },
    // 清空选线
    addClose() {
      this.$refs.ruleFormRef.resetFields()
      this.selKeys = []
      this.addcateList.cat_level = 0
      this.addcateList.cat_pid = 0
    },
    // 确定添加分类
    SureAddCate() {
      this.$refs.rules.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('categores', this.addcateList)
        if (res.meat.state !== 200) {
          return this.$message.error('添加分类失败')
        }
        this.$message.success('添加分类成功')
        this.getGoodsList()
        this.dialogVisible = false
      })
    },
  },
}
</script>

<style scoped>
tree-table {
  margin: 15px 0;
}
</style>
